package gui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnyUriEditableTableCell<S extends Object> extends AbstractEditableTableCell<S, String> {

	@Override
	protected void commitHelper(boolean losingFocus) {
		if( textField == null ) {
			return;
		}

		String input = textField.getText();
		if (input == null || input.length() == 0) {
			setText("");
			commitEdit(new String(""));

			return;
		}

		String URI_REGEX = "^[(https?|ftp|file)://]?[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
		Pattern p = Pattern.compile(URI_REGEX);
		Matcher m = p.matcher(input);

		/*
		 * In the case that the pattern was not matched 
		 */
		if(m.find() == false){
			if( losingFocus ) {
				cancelEdit();
			}
			textField.setStyle("-fx-background-color: #B80000; -fx-text-fill:white; ");
			return;
		}
		else{
			commitEdit(textField.getText());
			//updateItem(input, true);
		}
	}

	@Override
	protected String getString() {
		return getItem() == null ? "" : getItem().toString();
	}

}
